import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'app_colors.dart';
import 'app_text_styles.dart';

/// 主题类型枚举
enum ThemeType { light, dark }

/// 应用主题管理器
class AppTheme {
  static ThemeType _currentTheme = ThemeType.light;

  /// 获取当前主题类型
  static ThemeType get currentTheme => _currentTheme;

  /// 设置当前主题类型
  static set currentTheme(ThemeType themeType) {
    _currentTheme = themeType;
  }

  /// 是否是深色模式
  static bool get isDarkMode => _currentTheme == ThemeType.dark;

  /// 获取当前主题数据
  static ThemeData get theme {
    return isDarkMode ? darkTheme : lightTheme;
  }

  /// 根据当前主题类型获取颜色
  static Color getColor(Color lightColor, Color darkColor) {
    return isDarkMode ? darkColor : lightColor;
  }

  /// 获取主题下的文本样式
  static TextStyle getTextStyle(TextStyle lightStyle, TextStyle darkStyle) {
    return isDarkMode ? darkStyle : lightStyle;
  }

  /// 浅色主题
  static final ThemeData lightTheme = ThemeData(
    useMaterial3: true,
    brightness: Brightness.light,
    primaryColor: AppColors.lightPrimary,
    scaffoldBackgroundColor: AppColors.lightScaffoldBackground,
    cardColor: AppColors.lightCardBackground,
    dividerColor: AppColors.lightDivider,
    disabledColor: AppColors.lightTextSecondary,
    colorScheme: const ColorScheme.light(
      primary: AppColors.lightPrimary,
      secondary: AppColors.lightAccent,
      onPrimary: Colors.white,
      onSecondary: Colors.white,
      background: AppColors.lightBackground,
      onBackground: AppColors.lightText,
      surface: AppColors.lightCardBackground,
      onSurface: AppColors.lightText,
      error: AppColors.lightError,
      onError: Colors.white,
    ),
    textTheme: TextTheme(
      displayLarge: AppTextStyles.lightHeadline1,
      displayMedium: AppTextStyles.lightHeadline2,
      displaySmall: AppTextStyles.lightHeadline3,
      headlineMedium: AppTextStyles.lightSubtitle1,
      headlineSmall: AppTextStyles.lightSubtitle2,
      bodyLarge: AppTextStyles.lightBodyText1,
      bodyMedium: AppTextStyles.lightBodyText2,
      labelLarge: AppTextStyles.lightButton,
      bodySmall: AppTextStyles.lightCaption,
    ),
    appBarTheme: AppBarTheme(
      color: AppColors.lightPrimary,
      elevation: 0,
      iconTheme: const IconThemeData(color: Colors.white),
      titleTextStyle: AppTextStyles.lightHeadline3.copyWith(
        color: Colors.white,
      ),
      systemOverlayStyle: SystemUiOverlayStyle.light,
    ),
    buttonTheme: const ButtonThemeData(
      buttonColor: AppColors.lightPrimary,
      textTheme: ButtonTextTheme.primary,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
    ),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ElevatedButton.styleFrom(
        foregroundColor: Colors.white,
        backgroundColor: AppColors.lightPrimary,
        shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(8)),
        ),
        textStyle: AppTextStyles.lightButton,
      ),
    ),
    textButtonTheme: TextButtonThemeData(
      style: TextButton.styleFrom(
        foregroundColor: AppColors.lightPrimary,
        textStyle: AppTextStyles.lightButton.copyWith(
          color: AppColors.lightPrimary,
        ),
      ),
    ),
    iconTheme: const IconThemeData(color: AppColors.lightText),
    bottomNavigationBarTheme: const BottomNavigationBarThemeData(
      backgroundColor: AppColors.lightCardBackground,
      selectedItemColor: AppColors.lightPrimary,
      unselectedItemColor: AppColors.lightTextSecondary,
    ),
    cardTheme: const CardTheme(
      color: AppColors.lightCardBackground,
      elevation: 2,
      margin: EdgeInsets.all(8),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
    ),
    inputDecorationTheme: InputDecorationTheme(
      filled: true,
      fillColor: AppColors.lightCardBackground,
      hintStyle: AppTextStyles.lightCaption,
      contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
      border: const OutlineInputBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
        borderSide: BorderSide(color: AppColors.lightBorder),
      ),
      enabledBorder: const OutlineInputBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
        borderSide: BorderSide(color: AppColors.lightBorder),
      ),
      focusedBorder: const OutlineInputBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
        borderSide: BorderSide(color: AppColors.lightPrimary, width: 2),
      ),
      errorBorder: const OutlineInputBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
        borderSide: BorderSide(color: AppColors.lightError),
      ),
    ),
  );

  /// 深色主题
  static final ThemeData darkTheme = ThemeData(
    useMaterial3: true,
    brightness: Brightness.dark,
    primaryColor: AppColors.darkPrimary,
    scaffoldBackgroundColor: AppColors.darkScaffoldBackground,
    cardColor: AppColors.darkCardBackground,
    dividerColor: AppColors.darkDivider,
    disabledColor: AppColors.darkTextSecondary,
    colorScheme: const ColorScheme.dark(
      primary: AppColors.darkPrimary,
      secondary: AppColors.darkAccent,
      onPrimary: Colors.white,
      onSecondary: Colors.white,
      background: AppColors.darkBackground,
      onBackground: AppColors.darkText,
      surface: AppColors.darkCardBackground,
      onSurface: AppColors.darkText,
      error: AppColors.darkError,
      onError: Colors.white,
    ),
    textTheme: TextTheme(
      displayLarge: AppTextStyles.darkHeadline1,
      displayMedium: AppTextStyles.darkHeadline2,
      displaySmall: AppTextStyles.darkHeadline3,
      headlineMedium: AppTextStyles.darkSubtitle1,
      headlineSmall: AppTextStyles.darkSubtitle2,
      bodyLarge: AppTextStyles.darkBodyText1,
      bodyMedium: AppTextStyles.darkBodyText2,
      labelLarge: AppTextStyles.darkButton,
      bodySmall: AppTextStyles.darkCaption,
    ),
    appBarTheme: AppBarTheme(
      color: AppColors.darkPrimary,
      elevation: 0,
      iconTheme: const IconThemeData(color: Colors.white),
      titleTextStyle: AppTextStyles.darkHeadline3.copyWith(color: Colors.white),
      systemOverlayStyle: SystemUiOverlayStyle.light,
    ),
    buttonTheme: const ButtonThemeData(
      buttonColor: AppColors.darkPrimary,
      textTheme: ButtonTextTheme.primary,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
    ),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ElevatedButton.styleFrom(
        foregroundColor: Colors.white,
        backgroundColor: AppColors.darkPrimary,
        shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(8)),
        ),
        textStyle: AppTextStyles.darkButton,
      ),
    ),
    textButtonTheme: TextButtonThemeData(
      style: TextButton.styleFrom(
        foregroundColor: AppColors.darkPrimary,
        textStyle: AppTextStyles.darkButton.copyWith(
          color: AppColors.darkPrimary,
        ),
      ),
    ),
    iconTheme: const IconThemeData(color: AppColors.darkText),
    bottomNavigationBarTheme: const BottomNavigationBarThemeData(
      backgroundColor: AppColors.darkCardBackground,
      selectedItemColor: AppColors.darkPrimary,
      unselectedItemColor: AppColors.darkTextSecondary,
    ),
    cardTheme: const CardTheme(
      color: AppColors.darkCardBackground,
      elevation: 4,
      margin: EdgeInsets.all(8),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
    ),
    inputDecorationTheme: InputDecorationTheme(
      filled: true,
      fillColor: AppColors.darkCardBackground,
      hintStyle: AppTextStyles.darkCaption,
      contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
      border: const OutlineInputBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
        borderSide: BorderSide(color: AppColors.darkBorder),
      ),
      enabledBorder: const OutlineInputBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
        borderSide: BorderSide(color: AppColors.darkBorder),
      ),
      focusedBorder: const OutlineInputBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
        borderSide: BorderSide(color: AppColors.darkPrimary, width: 2),
      ),
      errorBorder: const OutlineInputBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
        borderSide: BorderSide(color: AppColors.darkError),
      ),
    ),
  );
}
